
// 获取相差的天数和小时
const DifferenceDaysandHours = (timestamp1, timestamp2) => {
	let diffMs = timestamp2 - timestamp1; // 相差的毫秒数 
	let diffDays = Math.floor(diffMs / (24 * 60 * 60 * 1000));
	// 相差的天数，向下取整 
	let diffHours = Math.floor((diffMs - diffDays * 24 * 60 * 60 * 1000) / (60 * 60 * 1000));
	// 相差的小时数，向下取整 
	let TimeDifference = {}
	// 对象赋值
	TimeDifference.diffDays = diffDays
	TimeDifference.diffHours = diffHours
	return TimeDifference
}

// 时间戳获取时分
const setTimeDivision = (TIME) => {
	return dayjs(TIME).format('HH:mm');
}


// 获取token
const uniGetToken = () => {
	try {
		const value = uni.getStorageSync('accessToken');
		if (value) {
			return value
		} else {
			return false
		}
	} catch (e) {
		// error
	}
}

// 时分秒转换
const calculateDiffTime = (startTime, endTime) => {

	let runTime = parseInt((endTime - startTime) / 1000);
	let year = Math.floor(runTime / 86400 / 365);
	runTime = runTime % (86400 * 365);
	let month = Math.floor(runTime / 86400 / 30);
	runTime = runTime % (86400 * 30);
	let day = Math.floor(runTime / 86400);
	runTime = runTime % 86400;
	let hour = Math.floor(runTime / 3600);
	runTime = runTime % 3600;
	let minute = Math.floor(runTime / 60);
	runTime = runTime % 60;
	let second = runTime;
	let result = {
		year,
		month,
		day,
		hour,
		minute,
		second
	};
	// console.log(`${hour}小时${minute}分${second}秒`);
	let res = `${hour}小时${minute}分${second}秒`
	return res;
};

const yearCalculateDiffTime = (startTime, endTime) => {
	let runTime = parseInt((endTime - startTime) / 1000);
	let year = Math.floor(runTime / 86400 / 365);
	runTime = runTime % (86400 * 365);
	let month = Math.floor(runTime / 86400 / 30);
	runTime = runTime % (86400 * 30);
	let day = Math.floor(runTime / 86400);
	runTime = runTime % 86400;
	let hour = Math.floor(runTime / 3600);
	runTime = runTime % 3600;
	let minute = Math.floor(runTime / 60);
	runTime = runTime % 60;
	let second = runTime;
	let result = {
		year,
		month,
		day,
		hour,
		minute,
		second
	};


	let res = `${day}天${hour}小时${minute}分${second}秒`
	return res;
};


// 重组数据
export function setRecombination(arr) {
	let list = [];

	arr.forEach(item => {
		if (item.id) {
			// 如果是父节点，创建一个新对象并初始化子节点为空数组
			list.push({
				id: item.id,
				name: item.name,
				child: []
			});
		}
	});

	arr.forEach(item => {
		if (item.parentId) {
			// 如果是子节点，找到对应的父节点并将其添加到父节点的子节点数组中
			let parent = list.find(parent => parent.id === item.parentId);
			parent.child.push({
				parentId: item.parentId,
				name: item.name
			});
		}
	});
	return list
}

// 重构二级联动数据给uview2 Picker使用
export function constructGroups(arr) {
    let columns = [[], []];
    let groupById = {};

    // 初始化 groupById
    arr.forEach(item => {
        if (item.id) {
            groupById[item.id] = [item.name]; // 初始化数组并添加id元素的name
        }
    });

    // 根据 parentId 填充 parentId
    arr.forEach(item => {
        if (item.parentId && groupById[item.parentId]) {
            groupById[item.parentId].push(item.name); // 将子元素name加入对应的父id组
        }
    });

    // 从 groupById 中构建 columnData
    let columnData = Object.values(groupById).map(group => group.slice(1)); // 去掉第一个元素

    // 构建 columns 数组
    arr.forEach(item => {
        if (item.id && !columns[0].includes(item.name)) {
            columns[0].push(item.name); // 第一列添加 id 对应的 name
        }
    });

    // 构建第二列数据
    arr.forEach(item => {
        if (columns[0].includes(item.name)) {
            let id = item.id;
            if (groupById[id]) {
                groupById[id].slice(1).forEach(name => { // 只取第二列数据
                    if (!columns[1].includes(name)) {
                        columns[1].push(name);
                    }
                });
            }
        }
    });
	console.log(columns);
	console.log(columnData);
    return {
        columns,
        columnData
    };
}
// 时间戳转 xxxx-xx-xx格式
export function formatTimestamp(timestamp) {
    const date = new Date(timestamp);

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，所以要加1
    const day = String(date.getDate()).padStart(2, '0');

    return `${year}-${month}-${day}`;
}
export function setTimes(timestamp){
  const date = new Date(timestamp)

  // 格式化为 yyyy-mm-dd hh:mm:ss
  return (
    date.getFullYear() +
    '-' +
    String(date.getMonth() + 1).padStart(2, '0') +
    '-' +
    String(date.getDate()).padStart(2, '0')
  )
}
export {
	DifferenceDaysandHours,
	setTimeDivision,
	calculateDiffTime,
	yearCalculateDiffTime,
	uniGetToken,
}